package org.dromara.web.service.business;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.dao.business.domain.SmartAgentSetting;
import org.dromara.dao.business.domain.SmartAgentSettingQuery;
import org.dromara.dao.business.mapper.SmartAgentSettingMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;

@Service
public class SmartAgentSettingService {

    @Autowired
    SmartAgentSettingMapper smartAgentSettingMapper;


    public TableDataInfo<SmartAgentSetting> queryPageList(SmartAgentSettingQuery query, PageQuery pageQuery) {
        return new TableDataInfo(
                smartAgentSettingMapper.paginationSelectList(query, pageQuery),
                smartAgentSettingMapper.paginationSelectCount(query)
        );
    }


    public SmartAgentSetting queryById(Long id) {
        return smartAgentSettingMapper.selectById(id);
    }


    @Transactional
    public SmartAgentSetting insert(SmartAgentSetting entity) {
        smartAgentSettingMapper.insert(entity);
        return entity;
    }

    @Transactional
    public void updateById(SmartAgentSetting update) {

        SmartAgentSetting entity = smartAgentSettingMapper.selectById(update.getId());
        if (entity == null) {
            throw new ServiceException();
        }
        entity.setAgentId(update.getAgentId());
        entity.setUserId(update.getUserId());
        entity.setNetworkSwitch(update.getNetworkSwitch());
        entity.setImageSwitch(update.getImageSwitch());
        entity.setDataSwitch(update.getDataSwitch());
        entity.setUpdateTime(LocalDateTime.now());
        smartAgentSettingMapper.updateById(entity);
    }


    @Transactional
    public void deleteById(Long id) {
        smartAgentSettingMapper.deleteById(id);
    }

    @Transactional
    public void deleteByIds(Long[] ids) {
        smartAgentSettingMapper.deleteByIds(Arrays.asList(ids));
    }


    public SmartAgentSetting findSetting(Long agentId, Long userId) {
        SmartAgentSetting smartAgentSetting = smartAgentSettingMapper.selectOne(new LambdaQueryWrapper<SmartAgentSetting>().eq(SmartAgentSetting::getAgentId, agentId).eq(SmartAgentSetting::getUserId, userId));
        if (smartAgentSetting == null) {
            smartAgentSetting = new SmartAgentSetting();
            smartAgentSetting.setNetworkSwitch(false);
            smartAgentSetting.setDataSwitch(false);
            smartAgentSetting.setImageSwitch(false);
            smartAgentSetting.setUserId(userId);
            smartAgentSetting.setAgentId(agentId);
            smartAgentSettingMapper.insert(smartAgentSetting);
        }
        return smartAgentSetting;
    }


    @Transactional
    public void updateNetworkSwitch(Long agentId, Long userId, boolean networkSwitch) {
        SmartAgentSetting smartAgentSetting = findSetting(agentId, userId);
        smartAgentSetting.setNetworkSwitch(networkSwitch);
        smartAgentSettingMapper.updateById(smartAgentSetting);
    }
    @Transactional
    public void updateImageSwitch(Long agentId, Long userId, boolean networkSwitch) {
        SmartAgentSetting smartAgentSetting = findSetting(agentId, userId);
        smartAgentSetting.setImageSwitch(networkSwitch);
        smartAgentSettingMapper.updateById(smartAgentSetting);
    }
    @Transactional
    public void updateDataSwitch(Long agentId, Long userId, boolean networkSwitch) {
        SmartAgentSetting smartAgentSetting = findSetting(agentId, userId);
        smartAgentSetting.setDataSwitch(networkSwitch);
        smartAgentSettingMapper.updateById(smartAgentSetting);
    }


    @Transactional
    public void updateAgentLastChatTime(Long userId, Long agentId) {
        final SmartAgentSetting smartAgentSetting = findSetting(agentId, userId);
        smartAgentSetting.setLastChatTime(LocalDateTime.now());
        smartAgentSettingMapper.updateById(smartAgentSetting);
    }

    @Transactional
    public void updateAgentTop(Long agentId,Long userId, Boolean top) {
        SmartAgentSetting smartAgentSetting = smartAgentSettingMapper.selectOne(new LambdaQueryWrapper<SmartAgentSetting>().eq(SmartAgentSetting::getAgentId, agentId).eq(SmartAgentSetting::getUserId, userId));
        if (smartAgentSetting == null) {
            smartAgentSetting = new SmartAgentSetting();
            smartAgentSetting.setTop(top);
            smartAgentSetting.setImageSwitch(false);
            smartAgentSetting.setNetworkSwitch(false);
            smartAgentSetting.setDataSwitch(false);
            smartAgentSetting.setAgentId(agentId);
            smartAgentSetting.setUserId(userId);
            smartAgentSettingMapper.insert(smartAgentSetting);
        } else {
            smartAgentSetting.setTop(top);
            smartAgentSettingMapper.updateById(smartAgentSetting);
        }
    }
}
